<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * @var Request
     */
    protected $request;

    function __construct(Request $request){
        $this->request = $request;
    }

    public function input($key){
        return $this->request->input($key);
    }

    public static function apiFail($msg, $callback = null, $ret = -1){
        $json = json_encode(['ret'=>$ret,'msg'=>$msg,'st'=>time()]);
        if(empty($callback)){
            return $json;
        }else{
            return $callback.'('.$json.')';
        }
    }

    public static function apiSucc($data='', $callback = null, $ret = 1){
        $json = json_encode(['ret'=>$ret,'data'=>$data,'st'=>time()]);
        if(empty($callback)){
            return $json;
        }else{
            return $callback.'('.$json.')';
        }
    }

    public static function jump($msg, $url, $level='info'){
        return self::hint($msg, false, $url, $level);
    }

    public static function back($msg, $level='info'){
        return self::hint($msg, true, null, $level);
    }

    public static function close($msg, $level='info'){
        return self::hint($msg, false, null, $level);
    }

    private static function hint($msg, $isShowBack, $url, $level){
        if(!in_array($level,['info','error','succ'])){
            $level = 'info';
        }
//        if(Util::isMobile()) {
//            return view('mobile.hint', ['msg' => $msg, 'isShowBack' => $isShowBack, 'url' => $url, 'type' => $level]);
//        }else {
//            return view('layout.hint', ['msg' => $msg, 'isShowBack' => $isShowBack, 'url' => $url, 'type' => $level]);
//        }
    }

    public static function jsonpSucc($data='', $callback, $ret = 1){
        $json = ['ret'=>$ret,'data'=>$data,'st'=>time()];
        $json = json_encode($json);
        return $callback.'('.$json.')';
    }

    public static function jsonFail($msg='', $callback, $ret = -1){
        $json = ['ret'=>$ret,'msg'=>$msg,'st'=>time()];
        $json = json_encode($json);
        return $callback.'('.$json.')';
    }

    /**
     * @deprecated
     * @param $msg
     * @param bool $isShowBack
     * @param null $url
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public static function error($msg, $isShowBack = false, $url=null){
        return view('layout.error',['msg'=>$msg,'isShowBack' => $isShowBack,'url'=>$url, 'type'=>'error']);
    }
}
